package com.lins.common.mp.handler;

import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.lins.common.mp.config.TenantProperties;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;

import javax.annotation.Resource;

/**
 * @author yuxiaolin
 * @title: TenantLineHandler
 * @projectName lins
 * @description: TODO
 * @date 2021/12/15 11:08 上午
 */

@Slf4j
public class LinSTenantLineHandler implements TenantLineHandler {

    private String tenantCode;

    @Resource
    private TenantProperties tenantProperties;

    private TenantInfoService tenantInfoService;

    public LinSTenantLineHandler(TenantInfoService tenantInfoService)
    {
        this.tenantCode =tenantInfoService.getTenantCode();
        this.tenantInfoService = tenantInfoService;
    }

    @Override
    public Expression getTenantId() {
        return new StringValue(tenantCode);
    }


    /**
    *
    * */
    @Override
    public String getTenantIdColumn() {

        return "tenantCode";
    }

    @Override
    public boolean ignoreTable(String tableName) {

        boolean isIgnoreTable = true;

        /*
        * 如过是租户,而且不包含忽略的表
        *
        *
        * */
        if (tenantInfoService.isTenant()&& !tenantProperties.getExclusionTable().contains(tableName))
        {
            isIgnoreTable =false;
        }
        return isIgnoreTable;
    }


}
